package com.example.demo.aws3;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.nio.file.Paths;
import java.util.List;

public class Demo {


    public static void main(String[] args) {


        String bucket_name = "uploadpic";
        String filePath = "D:\\testPic.png";
        String keyName = "testPic.png";

        System.out.format("Uploading %s to S3 bucket %s...\n", filePath, bucket_name);
        BasicAWSCredentials awsCreds = new BasicAWSCredentials("NZEFD8HC1UB534CAZJ5Q", "FcSBO8h4q5Bs4XkukfUKKPnKiBjJf91iAomzz0vH");
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(
                        "http://183.129.194.35:50071",
                        "us-east-1"))
                //.withRegion(Regions.AP_SOUTHEAST_1)
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                .build();
        try {
            List<Bucket> buckets = s3Client.listBuckets();
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }

        /*BasicAWSCredentials awsCreds = new BasicAWSCredentials("NZEFD8HC1UB534CAZJ5Q", "FcSBO8h4q5Bs4XkukfUKKPnKiBjJf91iAomzz0vH");
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                .withRegion(Regions.CN_NORTH_1)
                .build();

        try {
            s3Client.putObject(bucket_name, keyName, new File(filePath));
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }
        System.out.println("Done!");*/


        /*ClientConfiguration opts = new ClientConfiguration();
        //opts.setSignerOverride("S3SignerType");
        AWSCredentials credentials = new BasicAWSCredentials("NZEFD8HC1UB534CAZJ5Q", "FcSBO8h4q5Bs4XkukfUKKPnKiBjJf91iAomzz0vH");
        AmazonS3Client client = new AmazonS3Client(credentials);
        client.setEndpoint("183.129.194.35:50071");

        List<Bucket> buckets = client.listBuckets();
        for (Bucket b : buckets) {
            System.out.println(b.getName());
        }*/


        /*BasicAWSCredentials credentials = new BasicAWSCredentials("NZEFD8HC1UB534CAZJ5Q", "FcSBO8h4q5Bs4XkukfUKKPnKiBjJf91iAomzz0vH");
        //AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
        //        .withCredentials(new AWSStaticCredentialsProvider(credentials))
        //        .build();
        AmazonS3Client s3Client  = new AmazonS3Client(credentials);
        s3Client.setEndpoint("http://183.129.194.35:50071");
//        s3Client.setBucketEncryption()

        try {
            List<Bucket> buckets = s3Client.listBuckets();
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }

        s3Client.putObject(new PutObjectRequest(bucket_name, keyName, new File(filePath))
                .withCannedAcl(CannedAccessControlList.PublicRead));

        System.out.println(s3Client.getResourceUrl(bucket_name, keyName));

        //获取一个request
        GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
                bucket_name, keyName);
        //生成公用的url
        URL url = s3Client.generatePresignedUrl(urlRequest);
        System.out.println("URL:" + url);*/


     ;

    }
}
